<template>
  <div class="mod-shop-notice">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="keyName"
            :label="$t('sys.keyName') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.name"
              type="text"
              maxlength="30"
              clearable
              :placeholder="$t('sys.keyName')"
            />
          </el-form-item>
          <el-form-item
            prop="accessId"
            :label="$t('sys.accessId') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.accessId"
              type="text"
              maxlength="30"
              clearable
              :placeholder="$t('sys.accessId')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchFormRef')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('platform:sysAccessKey:save')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('sysManagement.add') }}
        </div>
      </div>
      <div class="table-con notice-table">
        <el-table
          ref="noticeListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            fixed
            align="left"
            prop="title"
            :label="$t('sys.keyName')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.name }}</span>
            </template>
          </el-table-column>

          <el-table-column
            fixed
            align="left"
            prop="accessId"
            :label="$t('sys.accessId')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.accessId }}</span>
            </template>
          </el-table-column>

          <el-table-column
            fixed
            align="left"
            prop="remark"
            :label="$t('crud.remark')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.remark ? scope.row.remark : '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('platform:sysAccessKey:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t('user.editBtn') }}
                </div>
                <div
                  v-if="isAuth('platform:sysAccessKey:reset')"
                  class="default-btn text-btn"
                  @click="resetKeyHandle(scope.row.sysAccessKeyId)"
                >
                  {{ $t('sys.resetKey') }}
                </div>
                <div
                  v-if="isAuth('platform:sysAccessKey:delete')"
                  class="default-btn text-btn"
                  @click.stop="onDelete(scope.row.sysAccessKeyId)"
                >
                  {{ $t('remindPop.delete') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { isAuth } from '@/utils/index.js'

let tempSearchForm = null // 保存上次点击查询的请求条件

const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  name: '',
  accessId: ''
})
const searchFormRef = ref(null)
const dataListLoading = ref(false)
const addOrUpdateRef = ref(null)
const addOrUpdateVisible = ref(false)

onMounted(() => {
  getDataList()
})
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/platform/sysAccessKey/page'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam == null ? page.currentPage : pageParam.currentPage,
      size: pageParam == null ? page.pageSize : pageParam.pageSize
    }, tempSearchForm))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    dataListLoading.value = false
  })
}
// 新增 / 修改
const onAddOrUpdate = (row) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(row ? row.sysAccessKeyId : 0, row ? row.sysType : 0)
  })
}
// 重置密钥
const resetKeyHandle = (id) => {
  ElMessageBox.confirm($t('sys.isResetKey'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/platform/sysAccessKey/resetAccessKey'),
      method: 'put',
      data: id
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}
const onDelete = (id) => {
  ElMessageBox.confirm($t('seckill.isDeleOper'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/platform/sysAccessKey/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}
// 刷新回调用
const refreshChange = () => {
  getDataList(page)
}
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

</script>

<style lang="scss" scoped>
</style>
